package com.zlsy.secondtime.background;

/**
 * @author zhouliang
 * @date 2020/5/25 20:11
 */
public class MultiThreadsError5 {
    int count;

    public MultiThreadsError5(MySource source) {
        //注册监听事件
        source.registerEventListener(new EventListener() {
            //匿名内部类持有外部类的引用，我们在这边可以直接对count进行打印或则修改都可以
            @Override
            public void onEvent(Event e) {
                //count的值取决于本类是否完成count=100的赋值
                System.out.println("我得到的数字是:" + count);
            }
        });

        for (int i = 0; i < 10000; i++) {
            System.out.print(i);
        }
        count = 100;
    }

    public static void main(String[] args) throws InterruptedException {
        MySource mySource = new MySource();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                mySource.enevtCome(new Event() {
                });
            }
        });
        Thread.sleep(10);
        thread.start();
        new MultiThreadsError5(mySource);
    }

    static class MySource {

        private EventListener eventListener;

        void registerEventListener(EventListener eventListener) {
            this.eventListener = eventListener;
        }

        void enevtCome(Event e) {
            if (eventListener != null) {
                eventListener.onEvent(e);
            } else {
                System.out.println("还未初始化完毕！");
            }
        }
    }

    interface Event {
    }

    interface EventListener {
        void onEvent(Event e);
    }
}